package us.versus.them.glassifried;

import flash.display.DisplayObject;
import flash.display.GradientType;
import flash.display.Graphics;
import flash.display.InterpolationMethod;
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.display.SpreadMethod;
import flash.display.Sprite;

import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;

import flash.text.StyleSheet;
import flash.text.TextField;

typedef FloatFormatter = Float -> String;

class ProgressBar extends Sprite {

	private var progress_ : Sprite;
	private var w_:Int;
	private var h_:Int;
	private var rgb_:Dynamic;

	private var floatFormatter:FloatFormatter;
	private var textField:TextField;
	private var hasSetTextField:Bool;

	public function new( x,y, w,h, r,g,b, ?floatFormatter:FloatFormatter ) {
		super();
		this.init( x,y, w,h, r,g,b, floatFormatter );
	}

	////

	public function init( x,y, w,h, r,g,b, ?floatFormatter:FloatFormatter ) {
		this.x = x;
		this.y = y;

		this.rgb_ = Glassing.toRGB( r,g,b );
		this.w_ = w;
		this.h_ = h;
		this.rgb_ = {r:r,g:g,b:b};

		this.progress_= new Sprite();
		this.progress_.x = 0;
		this.progress_.y = 0;

		addChild( Glassing.glass_it_up( 32, 32, 32, Math.floor(w),Math.floor(h), this.progress_ ) );

		if ( null != floatFormatter ) {
			this.floatFormatter = floatFormatter;
			var styleSheet = new StyleSheet();
			styleSheet.parseCSS( ''
				+ 'h1 {'
				+ '    font-family: Arial, Helvetica, sans-serif;'
				+ '    font-size: ' + Math.floor( h * 0.5 ) + 'px;'
				+ '    font-weight: 400;'
				+ '    color:red;'
				+ '}'
			);
			this.textField = Glassing.text( 'h1', '', '', w/2, 0, styleSheet );
			addChild( this.textField );
			this.hasSetTextField = false;
		}
	}

	// p : [0...1]
	public function move( p:Float ) {
		if ( p < 0 ) p = 0;
		if ( p > 1 ) p = 1;

		var h = this.h_;
		var w = Math.floor( this.w_ * p + h / 2 );

		if ( w > this.w_ ) w = this.w_;

		this.progress_.graphics.clear();

		Glassing.beginBaseGradient( this.progress_.graphics, this.rgb_.r,this.rgb_.g,this.rgb_.b, w,h );
		this.progress_.graphics.drawRoundRect( 0, 0 , w, h , h, h );
		this.progress_.graphics.endFill();

		if ( null != this.floatFormatter ) {
			this.textField.text = '<h1>' + this.floatFormatter( p ) + '</h1>';
			if ( true||this.hasSetTextField ) {
				// TODO: support other placement than centered
				this.textField.x = this.w_ / 2 - this.textField.width / 2;
				this.textField.y = this.h_ / 2 - this.textField.height / 2;
				this.hasSetTextField = true;
			}
			this.textField.filters = Glassing.WHITE_FILTER;
		}
	}

	public static function percent( p:Float ):String {
		return Math.floor( p * 100 ) + '%';
	}

	public static function percent2( p:Float ):String {
		return percentX( p, 2 );
	}

	public static function percentX( p:Float, precision:Int ):String {
		return (
			Math.floor( p * 100 ) 
			+ '.' + 
			Math.floor( 
				Math.pow( 10, precision ) * (
					p * 100 - Math.floor( p * 100 )
				)		
			)
			+ '%'
		);
	}
}
